package com.apestech.framework.xml.condition.modul;

import com.apestech.framework.json.JSONObject;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-23-10:29
 */
public class Control {

    private ArrayList<Field> fields=new ArrayList<Field>();
    private JSONObject fieldmap=new JSONObject();
    private boolean update=false;
    private String sort;
    private String upper;
    private String lower;
    private Integer range;
    private String keyField;
    private String ladderField;
    private ArrayList<String> resultField=new ArrayList<String>();
    private ArrayList<String> updateField=new ArrayList<String>();

    public void setControl(JSONObject map) throws Exception {
        if(map==null) return;
        sort=map.getString("sort");
        upper=map.getString("upper");
        lower=map.getString("lower");
        range=map.getInteger("range");
        keyField=map.getString("keyField");
    }

    public void addField(JSONObject map) throws Exception {
        String id=map.getString("field");
        Field f=getField(id);
        if(f==null) f=new Field();
        f.setField(map);
        setField(id, f);
        if(f.isLadder()){
            if(ladderField!=null && !id.equals(ladderField)){
                throw new Exception("请检查control部分的字段配置：阶梯区间类型字段只能定义一个！");
            }
            ladderField=id;
        }
        if(f.isResult()){
            if(f.isLadder()){
                throw new Exception("请检查control部分的字段配置：返回类型的字段不能同时为阶梯区间字段！");
            }
            if(f.isUpdate()){
                throw new Exception("请检查control部分的字段配置：返回类型的字段不能同时为更新类型字段！");
            }
            if(!resultField.contains(id)){
                resultField.add(id);
            }
        }
        if(f.isUpdate()){
            if(f.isLadder()){
                throw new Exception("请检查control部分的字段配置：更新类型的字段不能同时为阶梯区间字段！");
            }
            if(!updateField.contains(id)){
                updateField.add(id);
            }
        }
        if(!update && f.isUpdate()) update=true;
    }

    public Field getField(String id) throws Exception {
        Integer index=fieldmap.getInteger(id);
        if(index==null) return null;
        return fields.get(index);
    }

    private void setField(String id, Field f) throws Exception {
        Integer index=fieldmap.getInteger(id);
        if(index==null) {
            fieldmap.put(id, fields.size());
            fields.add(f);
        }
        else fields.set(index, f);
    }

    public ArrayList<Field> getFields() {
        return fields;
    }

    public boolean isUpdate() {
        return update;
    }

    public String getSort() {
        return sort;
    }

    public boolean isUpper() {
        if(upper==null) return false;
        return "1".equals(upper);
    }

    public boolean isLower() {
        if(lower==null) return false;
        return "1".equals(lower);
    }

    public Integer getRange() {
        return range;
    }

    public String getKeyField() {
        return keyField;
    }

    public String getLadderField() {
        return ladderField;
    }

    public ArrayList<String> getResultField() {
        return resultField;
    }

    public ArrayList<String> getUpdateField() {
        return updateField;
    }
}
